package com.woniu.adapter;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.sun.org.apache.regexp.internal.RE;
import com.woniu.client.ManagementClient;
import com.woniu.outlet.dao.EnergyMapper;
import com.woniu.outlet.dao.EquipmentMapper;
import com.woniu.outlet.dao.EquipmentStopMapper;
import com.woniu.outlet.po.Energy;
import com.woniu.outlet.po.Equipment;
import com.woniu.outlet.po.EquipmentStop;
import com.woniu.outlet.rabbitmq.MessageRepo;
import com.woniu.outlet.redis.RedisEquipmentRepo;
import com.woniu.service.EquipmentStopService;
import com.woniu.util.SnowflakeIdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class EquipmentAdapter {

    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private EquipmentStopMapper equipmentStopMapper;
    @Autowired
    private EnergyMapper energyMapper;
    @Autowired
    private RedisEquipmentRepo redisEquipmentRepo;
    @Autowired
    private MessageRepo messageRepo;
    @Autowired
    @Qualifier("equipmentManagement")
    private ManagementClient managementClient;

    //查询设备列表
    public List<Equipment> findEquipments() throws Exception{
        ObjectMapper mapper = new ObjectMapper();
        String str = redisEquipmentRepo.findEquipments("equipment:list");
        List<Equipment> lists = new ArrayList<>();
        if(!StringUtils.isEmpty(str)){
            CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, Equipment.class);
            lists = mapper.readValue(str,type);
            return lists;
        }
        lists = equipmentMapper.selectByExample(null);
        redisEquipmentRepo.saveEquipment("equipment:list",mapper.writeValueAsString(lists));

        //系统日志插入
        managementClient.addSystemLog("查询所有设备");
        return lists;
    }

    //查询设备(根据id)
    public Equipment findEquipmentById(Long id) throws Exception{
        ObjectMapper mapper = new ObjectMapper();
        String str = redisEquipmentRepo.findEquipments("equipment"+id);
        Equipment equipment = null;
        if (!StringUtils.isEmpty(str)){
            equipment = mapper.readValue(str, new TypeReference<Equipment>() {
            });
            return equipment;
        }
        equipment = equipmentMapper.selectByPrimaryKey(id);
        redisEquipmentRepo.saveEquipment("equipment"+id,mapper.writeValueAsString(equipment));

        //系统日志插入
        managementClient.addSystemLog("查询设备");
        return equipment;
    }

    //添加设备
    public void addEquipment(Equipment equipment) {
        SnowflakeIdGenerator snowId = new SnowflakeIdGenerator();
        equipment.setEquipmentNo("EQ-"+snowId.nextId());
        equipment.setMachineNo("MA-"+snowId.nextId());
        equipment.setEquipmentStatus("正常");
        equipment.setBuyDate(new Date());
        equipmentMapper.insert(equipment);
        redisEquipmentRepo.deleteEquipment("equipment:list");

        //系统日志插入
        managementClient.addSystemLog("添加设备");
    }

    //修改设备
    public Boolean updateEquipment(Equipment equipment) {
        if (equipment.getId() == null){
            return false;
        }
        Equipment equipment1 = equipmentMapper.selectByPrimaryKey(equipment.getId());
        if (equipment1 == null){
            return false;
        }
        equipmentMapper.updateByPrimaryKeySelective(equipment);
        redisEquipmentRepo.deleteEquipment("equipment:list");

        //系统日志插入
        managementClient.addSystemLog("修改设备");
        return true;
    }

    //删除设备
    public Boolean deleteEquipmentById(Long id) {
        if (id == null){
            return false;
        }
        Equipment equipment = equipmentMapper.selectByPrimaryKey(id);
        if (equipment == null){
            return false;
        }
        equipmentMapper.deleteByPrimaryKey(id);
        redisEquipmentRepo.deleteEquipment("equipment:list");
        redisEquipmentRepo.deleteEquipment("equipment"+id);

        //系统日志插入
        managementClient.addSystemLog("删除设备");
        return true;
    }

    //查询设备报停列表
    public List<EquipmentStop> findEquipmentStops() throws Exception{
        ObjectMapper mapper = new ObjectMapper();
        String str = redisEquipmentRepo.findEquipmentStops("equipment_stop:list");
        List<EquipmentStop> lists = new ArrayList<>();
        if(!StringUtils.isEmpty(str)){
            CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, EquipmentStop.class);
            lists = mapper.readValue(str,type);
            return lists;
        }
        lists = equipmentStopMapper.selectByExample(null);
        redisEquipmentRepo.saveEquipmentStop("equipment_stop:list",mapper.writeValueAsString(lists));

        //系统日志插入
        managementClient.addSystemLog("查询所有报停设备");
        return lists;
    }

    //查询设备报停(根据设备编码)
    public EquipmentStop findEquipmentStopByEquipmentNo(String equipmentNo) throws Exception{
        ObjectMapper mapper = new ObjectMapper();
        String str = redisEquipmentRepo.findEquipmentStops("equipment_stop"+equipmentNo);
        EquipmentStop equipmentStop = null;
        if (!StringUtils.isEmpty(str)){
            equipmentStop = mapper.readValue(str, new TypeReference<EquipmentStop>() {
            });
            return equipmentStop;
        }
        equipmentStop = equipmentStopMapper.selectEquipmentStopByEquipmentNo(equipmentNo);
        redisEquipmentRepo.saveEquipmentStop("equipment_stop"+equipmentNo,mapper.writeValueAsString(equipmentStop));
        //系统日志插入
        managementClient.addSystemLog("查询报停设备");
        return equipmentStop;
    }

    //查询设备报停(根据设备id)
    public EquipmentStop findEquipmentStopById(Long id) throws Exception{
        ObjectMapper mapper = new ObjectMapper();
        String str = redisEquipmentRepo.findEquipmentStops("equipment_stop"+id);
        EquipmentStop equipmentStop = null;
        if (!StringUtils.isEmpty(str)){
            equipmentStop = mapper.readValue(str, new TypeReference<EquipmentStop>() {
            });
            return equipmentStop;
        }
        equipmentStop = equipmentStopMapper.selectByPrimaryKey(id);
        redisEquipmentRepo.saveEquipmentStop("equipment_stop"+id,mapper.writeValueAsString(equipmentStop));

        //系统日志插入
        managementClient.addSystemLog("查询报停设备");
        return equipmentStop;
    }

    //添加设备报停
    public Boolean addEquipmentStop(EquipmentStop equipmentStop) {
        if (equipmentStop.getEquipmentNo() == null || equipmentStop.getEquipmentNo() =="") {
            return false;
        }
        EquipmentStop equipmentStop1 = equipmentStopMapper.selectEquipmentStopByEquipmentNo(equipmentStop.getEquipmentNo());
        if (equipmentStop1 != null){
            return false;
        }
        SnowflakeIdGenerator snowId = new SnowflakeIdGenerator();
        equipmentStop.setStopNo("ST-"+snowId.nextId());
        equipmentStop.setStopDate(new Date());
        equipmentStopMapper.insert(equipmentStop);
        redisEquipmentRepo.deleteEquipmentStop("equipment_stop:list");

        //系统日志插入
        managementClient.addSystemLog("添加报停设备");
        return true;
    }

    //修改设备报停
    public Boolean updateEquipmentStop(EquipmentStop equipmentStop) {
        if (equipmentStop.getId() == null) {
            return false;
        }
        EquipmentStop equipmentStop1 = equipmentStopMapper.selectByPrimaryKey(equipmentStop.getId());
        if (equipmentStop1 == null){
            return  false;
        }
        equipmentStopMapper.updateByPrimaryKeySelective(equipmentStop);
        redisEquipmentRepo.deleteEquipmentStop("equipment_stop:list");

        //系统日志插入
        managementClient.addSystemLog("修改报停设备");
        return true;
    }


    //删除设备报停(根据报停id)
    public Boolean deleteEquipmentStopById(Long id) {
        if (id == null){
            return false;
        }
        EquipmentStop equipmentStop = equipmentStopMapper.selectByPrimaryKey(id);
        if (equipmentStop == null){
            return false;
        }
        equipmentStopMapper.deleteByPrimaryKey(id);
        redisEquipmentRepo.deleteEnergy("energy:list");
        redisEquipmentRepo.deleteEquipmentStop("equipment_stop"+id);

        //系统日志插入
        managementClient.addSystemLog("删除报停设备");
        return true;
    }

    //查询能源列表
    public List<Energy> findEnergys() throws Exception{
        ObjectMapper mapper = new ObjectMapper();
        String str = redisEquipmentRepo.findEnergys("energy:list");
        List<Energy> lists = new ArrayList<>();
        if (!StringUtils.isEmpty(str)){
            CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class,Energy.class);
            lists = mapper.readValue(str,type);
            return lists;
        }
        lists = energyMapper.selectByExample(null);
        redisEquipmentRepo.saveEnergy("energy:list",mapper.writeValueAsString(lists));

        //系统日志插入
        managementClient.addSystemLog("查询能源列表");
        return lists;
    }

    //添加能源
    public void addEnergy(Energy energy) {
        SnowflakeIdGenerator snowId = new SnowflakeIdGenerator();
        energy.setEnergyNo("EN-"+snowId.nextId());
        SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now=new Date();
        energy.setStartDate(myFmt2.format(now));
        energyMapper.insert(energy);
        redisEquipmentRepo.deleteEnergy("energy:list");

        //系统日志插入
        managementClient.addSystemLog("添加能源");
    }

    //修改能源
    public Boolean updateEnergy(Energy energy) {
        if(energy.getId() == null){
            return false;
        }
        Energy energy1 = energyMapper.selectByPrimaryKey(energy.getId());
        if (energy1 == null){
            return false;
        }
        energyMapper.updateByPrimaryKeySelective(energy);
        redisEquipmentRepo.deleteEnergy("energy:list");

        //系统日志插入
        managementClient.addSystemLog("修改能源");
        return true;
    }

    //删除能源(根据id)
    public Boolean deleteEnergyById(Long id) {
        if (id == null){
            return false;
        }
        Energy energy = energyMapper.selectByPrimaryKey(id);
        if (energy == null){
            return false;
        }
        energyMapper.deleteByPrimaryKey(id);
        redisEquipmentRepo.deleteEnergy("energy:list");

        //系统日志插入
        managementClient.addSystemLog("删除能源");
        return true;
    }


}
